package com.luych.toolbox.gateway.center;

import com.luych.toolbox.common.constant.AnnotationConstant;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.bus.jackson.RemoteApplicationEventScan;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@SpringCloudApplication
@EnableScheduling
@EnableZuulProxy
@EnableWebSocket
@EnableFeignClients(basePackages = AnnotationConstant.FEIGN_PACKAGE)
@RemoteApplicationEventScan(basePackages = AnnotationConstant.EVENT_PACKAGE)
public class GatewayCenterApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayCenterApplication.class, args);
    }

    @Bean
    public ServerEndpointExporter serverEndpoint() {
        return new ServerEndpointExporter();
    }

    @Bean
    public Contact contact(Environment environment) {
        String authorName = environment.getProperty("author.name");
        String authorUrl = environment.getProperty("author.url");
        String authorEmail = environment.getProperty("author.email");
        return new Contact(authorName, authorUrl, authorEmail);
    }

    @Bean
    public Docket docket(@Value("${spring.application.name}") String application, Contact contact) {
        ApiInfoBuilder apiInfoBuilder = new ApiInfoBuilder();
        ApiInfo apiInfo = apiInfoBuilder
                .title(application.toUpperCase())
                .version("LATEST")
                .contact(contact)
                .build();

        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo)
                .enable(true)
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .build();
    }

}
